#include "stdafx.h"
#include "progress_example.h"
#include <QVBoxLayout>
#include <QHBoxLayout>
#include <QProgressBar>
#include <QPushButton>
#include <QLabel>
#include <QTimer>
#include <QMessageBox>
#include <QFont>
#include <QNetworkAccessManager>
#include <QNetworkReply>
ProgressExample::ProgressExample(QWidget* parent)
    : QWidget(parent), currentProgress(0)
{
    setupUI();
    setupConnections();
}

void ProgressExample::setupUI()
{
    setWindowTitle("Qt 进度条示例 - QProgressBar");
    setFixedSize(400, 200);

    QVBoxLayout* mainLayout = new QVBoxLayout(this);

    // 创建标题标签
    QLabel* titleLabel = new QLabel("嵌入式进度条演示");
    titleLabel->setAlignment(Qt::AlignCenter);
    QFont titleFont = titleLabel->font();
    titleFont.setPointSize(14);
    titleFont.setBold(true);
    titleLabel->setFont(titleFont);

    // 创建进度条
    progressBar = new QProgressBar();
    progressBar->setRange(0, 100);
    progressBar->setValue(0);
    progressBar->setFormat("%p%");
    progressBar->setTextVisible(true);

    // 状态标签
    statusLabel = new QLabel("准备就绪");
    statusLabel->setAlignment(Qt::AlignCenter);

    // 按钮
    startButton = new QPushButton("开始任务");
    resetButton = new QPushButton("重置");
    resetButton->setEnabled(false);

    QHBoxLayout* buttonLayout = new QHBoxLayout();
    buttonLayout->addWidget(startButton);
    buttonLayout->addWidget(resetButton);

    // 添加到主布局
    mainLayout->addWidget(titleLabel);
    mainLayout->addSpacing(20);
    mainLayout->addWidget(progressBar);
    mainLayout->addWidget(statusLabel);
    mainLayout->addSpacing(20);
    mainLayout->addLayout(buttonLayout);

    // 创建定时器
    timer = new QTimer(this);
}

void ProgressExample::setupConnections()
{
    connect(startButton, &QPushButton::clicked, this, &ProgressExample::startProgress);
    connect(resetButton, &QPushButton::clicked, this, &ProgressExample::resetProgress);
    connect(timer, &QTimer::timeout, this, &ProgressExample::updateProgress);
}

void ProgressExample::startProgress()
{
   
    // 创建网络管理器
    QNetworkAccessManager* manager = new QNetworkAccessManager(this);

    // 设置请求的URL
    QNetworkRequest request;
    request.setUrl(QUrl("https://sys-10-2.xiaoguaniu.com/win10/202509/ZJ_WIN10_X64_19045.6332.iso?t=1758631537&ip=223.74.170.29&sign=33a549c545ccdc6457247580f9c6b6e9"));

    // 发送GET请求，并获取表示此次回复的对象
    QNetworkReply* reply = manager->get(request);
   
 

    currentProgress = 0;
    progressBar->setValue(0);
    statusLabel->setText("任务进行中...");
    startButton->setEnabled(false);
    resetButton->setEnabled(true);

    timer->start(100);
}

void ProgressExample::resetProgress()
{
    timer->stop();
    currentProgress = 0;
    progressBar->setValue(0);
    statusLabel->setText("准备就绪");
    startButton->setEnabled(true);
    resetButton->setEnabled(false);
}

void ProgressExample::updateProgress()
{
    currentProgress += 2;
    progressBar->setValue(currentProgress);
    statusLabel->setText(QString("处理中... %1%").arg(currentProgress));

    if (currentProgress >= 100) {
        timer->stop();
        statusLabel->setText("任务完成!");
        startButton->setEnabled(true);
        resetButton->setEnabled(true);
        QMessageBox::information(this, "完成", "任务执行成功!");
    }
}

// 回调函数，将下载的数据写入文件
size_t write_data(void* ptr, size_t size, size_t nmemb, FILE* stream) {
    return fwrite(ptr, size, nmemb, stream);
}
